local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.GameObject(),script.Parent.Name.."_"..script.Name);
local ConnectClass=CommonOHOH.load.Connect();
local InstanceManager = CommonOHOH.load.InstanceManager();

function M.new(self, ...)
	return M(self, ...);
end;

--构造函数
function M.ctor(self,map,levelIdList,playerList,battleId)


    self.PlayerInstList=playerList --参与玩家列表
    self.BattleId=battleId --  战场ID
    self.LevelIdList=levelIdList --关卡Id

    self.WaveProgressUpdEvent=ConnectClass.new() --波次进度更新
    self.EndEvent=ConnectClass.new() -- 结束战斗,发送战斗结束的结果
    self.PlayerExit=ConnectClass.new() -- 有玩家退出战斗
    
    self.PlayerRemoveCon= game.Players.PlayerRemoving:Connect(function(player) --有玩家退出游戏
        self:PlayerInstRemoveForBattle(player)
    end)

    M.super.ctor(self,map);

    --添加公共控制器
    self:AddPrivateComponentByFolder(script.Parent.com)

   
end;


--析构函数
function M.dtor(self)

    self.WaveProgressUpdEvent:dtor()
    self.EndEvent:dtor()
    self.PlayerExit:dtor()
    if  self.PlayerRemoveCon then
        self.PlayerRemoveCon:Disconnect()
    end
    self.PlayerRemoveCon=nil --玩家离开到监听

	M.super.dtor(self);
end;

--为玩家寻找最近的攻击目标
function M.FindNearMonster(self,character,...)
   return  self:GetPrivateComponent("MonsterCtrl"):FindNearMonster(character,...)
end

--通过guid 寻找怪物对象
function M.findMonsterByGuid(self,guid)
   local obj=  self:GetPrivateComponent("AttCtrl"):FindMonsterFolder():FindFirstChild(guid)
   if obj then
        return InstanceManager:FindObject(obj)
   end
end




--设置波次信息
function M.SetWaveProgress(self,now,all,monsterCount)
    self.attribute.killed=0
    self.attribute.WaveIndex=now
    self.attribute.WaveCount=all
    self.attribute.MonsterCount=monsterCount
    self.WaveProgressUpdEvent:Action()
end

--设置击杀数量
function M.AddKilledCount(self,add)
    self.attribute.killed=self.attribute.killed+add
end

-- function M.ClearKilled(self)
--     self.attribute.killed=0
-- end

--  返回战斗地图
function M.GetMap(self)
    return self.main_inst
end

--返回关卡列表
function M.GetLevelList(self)
    return self.LevelIdList

end

-- 返回玩家用户Id列表
function M.GetPlayerUserIdList(self)

  local list={}

   for player,v in pairs(self.PlayerInstList) do
        table.insert(list,player.UserId)
   end

   return list
end

-- 返回战斗ID
function M.GetId(self)
    return self.BattleId
end

--玩家离开战斗
function M.PlayerInstRemoveForBattle(self,player)

    if  self.PlayerInstList[player] then
        self.PlayerInstList[player]=nil

        self.PlayerExit:Action(player)

        if self:BattPlayerInstCount()==0 then
            self:End(false)
        end
    end
end


--是否存在玩家
function M.IsHadPlayerInst(self,playerInst)
    return  self.PlayerInstList[playerInst]
end

--获取当前战斗中的玩家数量
function M.BattPlayerInstCount(self)
    local sum=0
    for k,v in pairs(self.PlayerInstList) do
        sum=sum+1
    end
    return sum
end

--获取战斗中的所有玩家
function M.GetAllPlayerList(self)
    local list ={}

    for inst,v in pairs(self.PlayerInstList) do
        -- print("-----GetAllPlayerList-------:",inst,v)
        local player= InstanceManager:FindObject(inst)
        if player then
            table.insert(list,player)
        end
    end
    return list
end

-- 战斗结束(传入是否胜利)
function M.End(self,isWin)
    self.EndEvent:Action(isWin)
    self:dtor()
end



--获取波次信息
function M.GetWaveInfo(self)
    return {
        WaveIndex=self.attribute.WaveIndex,
        WaveCount=self.attribute.WaveCount,
        MonsterCount= self.attribute.MonsterCount
    }
end

--回溯是由准备对象（准备对象开启了这场战斗）
function M.FindReadyObj(self)
    _G.client:GetCompent()
end

--开始一场战斗（由子类去实现）
function M.Action(self)
 
end




return M;
